package localhost.teste;

import java.util.ArrayList;
import java.util.List;

import javax.naming.NamingException;
import javax.persistence.*;

import localhost.model.Categoria;

public class TesteConexao {
	private static EntityManagerFactory emf;
	private static EntityManager em;

	public static void startConnection() {
		emf = Persistence.createEntityManagerFactory("orcamento");
		em = emf.createEntityManager();
		em.getTransaction().begin();
	}

	public static void closeConnection() {
		em.getTransaction().commit();
		emf.close();
	}

	public static void main(String[] args) throws Exception {
		listaCategorias();
	}

	@SuppressWarnings("unchecked")
	public static void listaCategorias() throws NamingException {
		startConnection();

		List<Categoria> lista = new ArrayList<Categoria>();

		String query = "SELECT E FROM localhost.model.Categoria E WHERE E.tipo<>'E' order by E.descricao";
		Query q = em.createQuery(query);
		lista = q.getResultList();
		System.out.println("tamanho da lista: \t" + lista.size());

		if (lista.size() > 0) {
			for (Categoria cats : lista) {
				System.out.println(cats.getDescricao());
			}
		}
		
		closeConnection();
		System.out.println("===============================");

	}

}